package com.example.safehome;

import android.os.Bundle;
import android.widget.ListView;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class FireRecordActivity extends AppCompatActivity {
    private List<FireWarningRecord>recordList=new ArrayList<>();
    private SwipeRefreshLayout swipeRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fire_activity_record);
        initrecord();
        FireAdapter adapter=new FireAdapter(FireRecordActivity.this,R.layout.fire_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_fire_record);
        listView.setAdapter(adapter);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    private void refreshRecord(){

        new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    Thread.sleep(2000);
                }catch (InterruptedException e){
                    e.printStackTrace();;
                }
                runOnUiThread(new Runnable(){
                    @Override
                    public void run(){
                        initrecord();
                        FireAdapter adapter=new FireAdapter(FireRecordActivity.this,R.layout.fire_inresource,recordList);
                        adapter.notifyDataSetChanged();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    @Override
    protected void onStart(){

        super.onStart();
        initrecord();
        FireAdapter adapter=new FireAdapter(FireRecordActivity.this,R.layout.fire_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_fire_record);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onResume(){

        super.onResume();
        initrecord();
        FireAdapter adapter=new FireAdapter(FireRecordActivity.this,R.layout.fire_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_fire_record);
        listView.setAdapter(adapter);
    }
    @Override
    protected void onRestart(){

        super.onRestart();
        initrecord();
        FireAdapter adapter=new FireAdapter(FireRecordActivity.this,R.layout.fire_inresource,recordList);
        ListView listView=(ListView)findViewById(R.id.lv_fire_record);
        listView.setAdapter(adapter);
    }
        //优化火灾检测记录展示
    private void initrecord(){

        List<FireWarningRecord> records= LitePal.findAll(FireWarningRecord.class);
        recordList.clear();
        for(FireWarningRecord record:records){
            recordList.add(record);
        }

    }
}
